package com.multiplay;

import java.io.IOException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.Selector;

import android.net.DhcpInfo;
import android.net.wifi.WifiManager;

public class WiFiLoop 
{
    protected boolean stop = false;

    protected Selector selector = null;

    protected int ipAddress;
    protected int netmask;
    
    protected DatagramChannel broadcastChannel = null;

    
    enum State { IDLE, SEARCH, CONNECT, IPC };
    State state;

    public WiFiLoop(WifiManager wifiManager) throws IOException
    {
        selector = Selector.open();
        state = State.IDLE;

        if (!wifiManager.isWifiEnabled())
            return;
        
        DhcpInfo dhcp = wifiManager.getDhcpInfo();
        if (dhcp != null) {
	        ipAddress = dhcp.ipAddress;
	        netmask = dhcp.netmask;
        }
        
    }

    
    public void startDiscovery() throws IOException {
        broadcastChannel = DatagramChannel.open();
        broadcastChannel.configureBlocking(false);

    }
    


    public synchronized void stop_loop()
    {
        stop = true;
        if (selector != null)
            selector.wakeup();
    }
}
